package com.jintoufs.weixin.order;

import com.jintoufs.domain.customer.Customer;
import com.jintoufs.service.hotel.HotelOrderService;
import com.jintoufs.service.order.OrderService;
import com.jintoufs.service.scene.SceneOrderService;
import com.jintoufs.service.tour.TourRouteOrderService;
import com.jintoufs.util.valid.ValidUtil;
import com.jintoufs.weixin.SupportAction;
import io.swagger.annotations.Api;
import org.cosmos.modules.web.msg.AjaxMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value = "订单相关接口")
@RestController
@RequestMapping("/api/order-v2")
public class OrderV2Action extends SupportAction {
    @Resource
    private OrderService orderService;
    @Autowired
    private SceneOrderService sceneOrderService;
    @Autowired
    private HotelOrderService hotelOrderService;
    @Autowired
    private TourRouteOrderService tourRouteOrderService;

    @GetMapping("/getOrderList")
    public AjaxMsg getOrderList(Integer pageSize, Integer pageNum, String status, String token) {
        AjaxMsg ajaxMsg = new AjaxMsg();
        Customer customer = getCustomerByToken(token);
        if (customer == null) {
            ajaxMsg.setCode(AjaxMsg.FAILURE);
            ajaxMsg.setMessage("用户验证出现了问题，请重新登陆");
        } else {
            Map<String, Object> params = new HashMap<>();
            params.put("pageStart", (pageNum - 1) * pageSize);
            params.put("pageSize", pageSize);
            if (!ValidUtil.isEmpty(status)) {
                params.put("status", status);
            }
            params.put("customerId", customer.getId());
            List<Map<String, Object>> result = orderService.getByParams(params);
            ajaxMsg.setDatas(result);
        }
        return ajaxMsg;
    }

    @GetMapping(value = "/updateOrderNew")
    AjaxMsg updateOrderNew(String token, String status) {
        AjaxMsg ajaxMsg = new AjaxMsg();
        Customer customer = getCustomerByToken(token);
        if (customer == null) {
            ajaxMsg.setCode(AjaxMsg.FAILURE);
            ajaxMsg.setMessage("用户验证出现了问题，请重新登陆");
        } else {
            Map params = new HashMap();
            params.put("customerId", customer.getId());
            params.put("status", status);
            sceneOrderService.updateOrderNew(params);
            hotelOrderService.updateOrderNew(params);
            tourRouteOrderService.updateOrderNew(params);
            ajaxMsg.setCode(AjaxMsg.SUCCESS);
        }
        return ajaxMsg;
    }

    @GetMapping(value = "/getOrderNew")
    AjaxMsg getOrderNew(String token) {
        AjaxMsg ajaxMsg = new AjaxMsg();
        Customer customer = getCustomerByToken(token);
        if (customer == null) {
            ajaxMsg.setCode(AjaxMsg.FAILURE);
            ajaxMsg.setMessage("用户验证出现了问题，请重新登陆");
        } else {
            Map params = new HashMap();
            params.put("customerId", customer.getId());
            params.put("status", 0);
            Integer status0 = orderService.getNewOrderCount(params);
            params.put("status", 1);
            Integer status1 = orderService.getNewOrderCount(params);
            params.put("status", 6);
            Integer status6 = orderService.getNewOrderCount(params);
            params.put("status", 3);
            Integer status3 = orderService.getNewOrderCount(params);
            Map result = new HashMap<>();
            result.put("status0", status0);
            result.put("status1", status1);
            result.put("status6", status6);
            result.put("status3", status3);
            ajaxMsg.setDatas(result);
            ajaxMsg.setCode(AjaxMsg.SUCCESS);
        }
        return ajaxMsg;
    }
}
